package com.fingard.dsp.bank.directbank.bojz01;

import java.util.HashMap;
import java.util.Map;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOJZ01Balance extends BOJZ01Base implements IBalance {

    //单个余额查询和批量余额查询，统一采用流处理方式
    //1、循环调用Req7506BalQry.readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用RetAts7506BalQry.writeRetDetail()，写入返回ATS的明细
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {//余额查询
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
        try {
            //赋值通用参数
            transCode = "B2EActBalQry";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            Map<String, Object> map = new HashMap<String, Object>();
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, "0");
            xmlWriter.writeStartElement("Body");
            while (p_req7506.readRecord()) {
                xmlWriter.writeElementString("AcNo", p_req7506.readedDetail.actNum);
                map.put("AcNo", p_req7506.readedDetail.actNum);
            }
            xmlWriter.writeEndDocument();
            //发送给银行的签名端口进行签名
            String[] tmpSign = sendToBankFrontSign(xmlWriter.toXmlString());
            if (tmpSign[0].length() == 0) {
                String signature = "";
                String body = "";
                String aa = "<result>";
                int i = tmpSign[1].indexOf(aa);
                if (i != -1) {
                    body = tmpSign[1].substring(i + 8, i + 9);
                }
                if (body.equals("0")) {
                    String ab = "<sign>";
                    String ac = "</sign>";
                    int ib = tmpSign[1].indexOf(ab);
                    int ic = tmpSign[1].indexOf(ac);
                    //以下为最终的签名结果
                    signature = tmpSign[1].substring(ib + 6, ic);
                    signature = clearLineWrap(signature);
                    singData = signature;

                    XmlTextWriter xmlTextWriter = new XmlTextWriter();
                    writeCommonHeader(xmlTextWriter, "1");
                    xmlTextWriter.writeStartElement("Body");
                    xmlTextWriter.writeElementString("AcNo", (String) map.get("AcNo"));
                    xmlTextWriter.writeEndDocument();
                    String[] tmpStrRet = sendToBank(xmlTextWriter.toXmlString());
                    if (tmpStrRet[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();

                        XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1].substring(tmpStrRet[1].indexOf("<?xml")), tmpSbMsg);

                        if (tmpBkReader != null) {
                            String tmpStatus = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                            String tmpStatusText = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                            if (tmpStatus != null && tmpStatus.equals("000000")) {
                                while (tmpBkReader.read("/Message/Body")) {
                                    tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("Currency"));
                                    tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AcNo");
                                    tmpRetDetail.respCode = "0";
                                    tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("AvailBal");
                                    tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Balance");
                                }
                            } else {
                                tmpRetDetail.respCode = "-1";
                                tmpRetDetail.respInfo =tmpStatusText;
                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo =tmpSbMsg.toString();
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo =tmpStrRet[1];
                    }
                } else {//签名数据返回数据为空
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSign[1];
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpSign[1];
            }
            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }

}
